package com.gxuwz.gj.action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.gxuwz.javabean.Basedata;

public class MainProcess2 {
	public static void main(String[] args) {
		//读取数据库,先获取 物料编码和销售组描述
		// 得到这些之后,再生成相同 物料编码和销售组描述 相同的数据的累加
		try {
			String sql = "select WLBM, XSZMS from basedata";
		//	String sql = "select * from basedata";
			//  得到所有的数据,把销售组描述 和  物料编码 单独放到list
			ResultSet rs = MainProcess2.getResultSet(sql);
			//用set去除重复
			Set<String> wlbmList = new HashSet<String>();
			Set<String> xszmsList = new HashSet<String>();
//			System.out.println("--------------------------");
			if (null != rs) {
				while (rs.next()) {
	//					System.out.println(rs.getRow());
					wlbmList.add(rs.getString("WLBM"));
					xszmsList.add(rs.getString("XSZMS"));
//					System.out.println("WLBM: " + rs.getString("WLBM") + ", XSZMS: " + rs.getString("XSZMS"));
				}
			}
//			System.out.println(wlbmList.size());
//			System.out.println(xszmsList.size());
//			System.out.println("======================");
			long startTime = System.currentTimeMillis() / 1000;
			System.out.println("开始计算时间: " + startTime);  //秒
			//float[] chje_sum = new float[]{};
			List<Float> chje_sum = new ArrayList<Float>();
			
			String sql2 = "select * from basedata";
			ResultSet rs2 = MainProcess2.getResultSet(sql2);
			List<Basedata> bs = initList4Database(rs2);
			System.out.println(bs.size());
			// 计算相同销售组描述\物料编码数据累计
			for (String str : xszmsList) {  //遍历 销售组描述
				for (String str2 : wlbmList) {  //遍历物料描述
					float chje = 0f;
					for (Basedata b : bs) {  //遍历所有的数据
						if (str.equals(b.getXSBMMS()) &&   //广西
								str2.equals(b.getWLBM())) {  //物料代码
	//						// 出货金额累加
							chje += b.getCHJE();
						}
					}
					chje_sum.add(chje);
				}
			}
			long endTime = System.currentTimeMillis() / 1000;
			System.out.println("介绍计算时间: " + endTime);  //秒
			System.out.println("花费时间: " + (endTime - startTime));
			System.out.println("=============================================");
			for (float f : chje_sum) {
				System.out.println(f);
			}
 		} catch (SQLException e) {
			e.printStackTrace();
		}
			
	}

	private static List<Basedata> initList4Database(ResultSet rs1)
			throws SQLException {
		List<Basedata> bds = new ArrayList<Basedata>();
		
		while (rs1.next()) {  //计算累加
			Basedata bd = new Basedata();
			bd.setCB(rs1.getFloat("CB"));
			bd.setCHJE(rs1.getFloat("CHJE"));
			bd.setCJFPRQ(rs1.getDate("CJFPRQ"));
			bd.setFKF(rs1.getString("FKF"));
			bd.setFKFMS(rs1.getString("FKFMS"));
			bd.setFZYG(rs1.getString("FZYG"));
			bd.setJHD(rs1.getString("JHD"));
			bd.setJJZ(rs1.getFloat("JJZ"));
			bd.setKPPZ(rs1.getString("KPPZ"));
			bd.setKPSL(rs1.getFloat("KPSL"));
			bd.setPPLX(rs1.getString("PPLX"));
			bd.setSDF(rs1.getString("SDF"));
			bd.setSDFMS(rs1.getString("SDFMS"));
			bd.setSE(rs1.getFloat("SE"));
			bd.setWLBM(rs1.getString("WLBM"));
			bd.setWLMS(rs1.getString("WLMS"));
			bd.setWLZ1MS(rs1.getString("WLZ1MS"));
			bd.setWLZ2MS(rs1.getString("WLZ2MS"));
			bd.setXM(rs1.getString("XM"));
			bd.setXSBM(rs1.getString("XSBM"));
			bd.setXSBMMS(rs1.getString("XSBMMS"));
			bd.setXSDD(rs1.getString("XSDD"));
			bd.setXSDW(rs1.getString("XSDW"));
			bd.setXSJG(rs1.getString("XSJG"));
			bd.setXSZ(rs1.getString("XSZ"));
			bd.setXSZMS(rs1.getString("XSZMS"));
			bd.setZK(rs1.getFloat("ZK"));
			bd.setCHJE(rs1.getFloat("CHJE"));
			
			bds.add(bd);
		}
		
		return bds;
	}
	
	/**
	 * 删除最后一个"\"
	 * @param string
	 * @return
	 */
	private static String doFlag(String str) {
		if (null != str) {
			if ("\\".equals(str.substring(str.length()-1))) {
				return str.substring(0, str.length()-2);
			}
		}
		
		return "";
	}

	public static ResultSet getResultSet(String sql) {
		ResultSet rs = null;
		Connection con = null;
		try {
			con = MainProcess2.getCon();
			PreparedStatement pstat = con.prepareStatement(sql);
			rs = pstat.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
//			if (null != con) {
//				try {
//					con.close();
//				} catch (SQLException e) {
//					e.printStackTrace();
//				}
//			}
		}
		
		return rs;
	}
	
	public static Connection getCon() {
		String url = "jdbc:mysql://localhost:3306/tj?useUnicode=true&characterEncoding=utf-8";
		String user = "root";
		String pwd = "";
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(url, user, pwd);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return con;
	}
}
